package com.wh.wisdomsite.xbox.server.controller.ba;

import com.wh.wisdomsite.xbox.ba.entity.Region;
import com.wh.wisdomsite.xbox.ba.service.IRegionService;
import com.wh.wisdomsite.xbox.common.util.ResManager;
import com.wh.wisdomsite.xbox.utils.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 公共区域controller
 * @author 刘旭
 * @version V1.0
 * @Package com.wh.wisdomsite.xbox.server.controller.ba
 * @date 2018年08月06日 22:35
 * @Copyright 企业版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2018
 */
@Controller
@RequestMapping(value = "/region")
public class RegionController {
    private static final Log logger = LogFactory.getLog(RegionController.class);

    //省份
    private static final String PROVINCE = "1";
    //城市
    private static final String CITY = "2";
    //区县
    private static final String DISTRICT = "3";

    @Autowired
    private IRegionService iRegionService;

    /**
     * 页面跳转
     * @return
     */
    @RequiresPermissions("rules:ba_region_page")
    @RequestMapping(value = "index.do", method = RequestMethod.GET)
    public String index(){
        return "ba/region/regionList";
    }

    /**
     * 通过类型和区域id获取省、市、区县集合
     * @param response
     * @param type
     * @param codeId
     */
    @RequiresPermissions("rules:ba_region_find")
    @RequestMapping(value = "/regionList.do", method = RequestMethod.GET)
    public void showRegionList(HttpServletResponse response,String type,String codeId){
        List<Region> regionList = new ArrayList<Region>();
        try{
            if(PROVINCE.equals(type)){
                regionList = iRegionService.getProvinceList(codeId);
            }
            if(CITY.equals(type)){
                regionList = iRegionService.getCityList(codeId);
            }
            if(DISTRICT.equals(type)){
                regionList = iRegionService.getDistrictList(codeId);
            }
            JSONArray array = new JSONArray();
            if(regionList.size() > 0){
                for (Region reg : regionList) {
                    JSONObject j = new JSONObject();
                    j.put("id",reg.getCodeId());
                    if(PROVINCE.equals(type)){
                        j.put("text",reg.getProvince());
                    }
                    if(CITY.equals(type)){
                        j.put("text",reg.getCity());
                    }
                    if(DISTRICT.equals(type)){
                        j.put("text",reg.getDistrict());
                    }
                    array.put(j);
                }
            }
            ResManager.getJsonOut(response).print(array);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 获取树形省、市
     * @param response
     * @param
     */
    @RequestMapping(value = "/getTreeRegion.do")
    public void getTreeRegion(HttpServletResponse response){
        try {
            logger.info("加载树形区域-->>开始");
            List<Region> regionList = iRegionService.getProvinceList(null);
            List<Region> cityList = iRegionService.getCityList(null);
            JSONArray array = new JSONArray();
            if(regionList.size() > 0){
                for(Region region : regionList){
                    String parentId = String.valueOf(region.getRegionId());
                    JSONObject object = new JSONObject(region);
                    JSONArray cityArray = new JSONArray();
                    for(Region city : cityList){
                        String cityId = city.getParentId();
                        if(cityId.equals(parentId)){
                            JSONObject cityObj = new JSONObject(city);
                            cityObj.put("province",city.getCity());
                            cityObj.put("state","closed");
                            cityArray.put(cityObj);
                        }
                    }
                    object.put("children",cityArray);
                    array.put(object);
                }
            }
            ResManager.getTextOut(response).print(array);
            logger.info("加载树形区域-->>结束");
        }catch (Exception e){
            e.printStackTrace();
            logger.debug("加载树形区域-->>Exception" + e.getMessage());
        }
    }

    /**
     * 获取树形区县
     * @param response
     * @param regionId
     * @param
     */
    @RequestMapping(value = "/getTreeDistrict.do")
    public void getTreeDistrict(HttpServletResponse response,String regionId){
        try {
            logger.info("加载树形区县-->>开始");
            List<Region> districtList = iRegionService.getDistrictList(regionId);
            JSONArray array = new JSONArray();
            if(districtList.size() > 0){
                for(Region region : districtList){
                    JSONObject object = new JSONObject(region);
                    object.put("province",region.getDistrict());
                    array.put(object);
                }
            }
            ResManager.getTextOut(response).print(array);
            logger.info("加载树形区县-->>结束");
        }catch (Exception e){
            e.printStackTrace();
            logger.debug("加载树形区县-->>Exception" + e.getMessage());
        }
    }

    /**
     * 区域详情
     * @param id
     * @param request
     * @return
     */
    @RequestMapping("regionDesc.do")
    public String appSecretDesc(String id, HttpServletRequest request) {
        logger.info("区域详情-->>开始");
        Region region = iRegionService.selectById(id);
        Region resetRegion = getParentRegiong(region);
        request.setAttribute("region", resetRegion);
        logger.info("区域详情-->>结束");
        return "/ba/region/regionDesc";
    }

    /**
     * 根据一个节点，查询所有节点后组合
     * @param region
     * @return
     */
    public Region getParentRegiong(Region region){
        String city = region.getCity();
        String district = region.getDistrict();
        String parentId = region.getParentId();
        if("0".equals(parentId)){
            return region;
        }else{
            if(StringUtils.isNotEmpty(district)){
                //查询城市
                Region cityEntity = iRegionService.selectById(parentId);
                String cityParentId = cityEntity.getParentId();
                String cityName = cityEntity.getCity();
                region.setCity(cityName);
                //查询省份
                Region provinceEntity = iRegionService.selectById(cityParentId);
                String provinceName = provinceEntity.getProvince();
                region.setProvince(provinceName);
            }
            if(StringUtils.isNotEmpty(city)){
                //查询省份
                Region provinceEntity = iRegionService.selectById(parentId);
                String provinceName = provinceEntity.getProvince();
                region.setProvince(provinceName);
            }
        }
        return region;
    }





}
